﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class VIPExchangePartDAL
    {
        public static int Add(VIPExchangePartInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_Add");
            DB.AddInParameter(dbCommand, "ItemCode", DbType.String, obj.ItemCode);
            DB.AddInParameter(dbCommand, "ItemName", DbType.String, obj.ItemName);
            DB.AddInParameter(dbCommand, "Price", DbType.Decimal, obj.Price);
            DB.AddInParameter(dbCommand, "GuidePrice", DbType.Decimal, obj.GuidePrice);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static int AddLastPrice(VIPExchangePartLastPriceInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_AddLastPrice");
            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
            DB.AddInParameter(dbCommand, "ItemCode", DbType.String, obj.ItemCode);
            DB.AddInParameter(dbCommand, "ItemName", DbType.String, obj.ItemName);
            DB.AddInParameter(dbCommand, "Price", DbType.Decimal, obj.Price);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static DataTable GetByWhere(string WhereSql)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_GetByWhere", WhereSql);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        public static IDataReader GetList(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_GetList", ID);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetListLastPrice(int UserID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_GetListLastPrice", UserID);
            return DB.ExecuteReader(dbCommand);
        }
        public static bool Update(VIPExchangePartInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_Update");
            DB.AddInParameter(dbCommand, "ID", DbType.Int32, obj.ID);
            DB.AddInParameter(dbCommand, "ItemCode", DbType.String, obj.ItemCode);
            DB.AddInParameter(dbCommand, "ItemName", DbType.String, obj.ItemName);
            DB.AddInParameter(dbCommand, "Price", DbType.Decimal, obj.Price);
            DB.AddInParameter(dbCommand, "GuidePrice", DbType.Decimal, obj.GuidePrice);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static bool UpdateLastPrice(VIPExchangePartLastPriceInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPExchangePart_UpdateLastPrice");
            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
            DB.AddInParameter(dbCommand, "ItemCode", DbType.String, obj.ItemCode);
            DB.AddInParameter(dbCommand, "ItemName", DbType.String, obj.ItemName);
            DB.AddInParameter(dbCommand, "Price", DbType.Decimal, obj.Price);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
